package com.dancun.controller;


import com.dancun.entity.Problem;
import com.dancun.entity.Solve;
import com.dancun.entity.TGroup;
import com.dancun.service.IProblemService;
import com.dancun.service.ISolveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController
public class SolveController {
    @Autowired
    ISolveService solveService;
    @Autowired
    IProblemService problemService;

    @RequestMapping("listSolve")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("/Main/ProblemSolve.jsp");
        List<Solve> data = solveService.list();
        if (data.size() != 0) {
            mav.addObject("solveflag", 1);
        } else {
            mav.addObject("solveflag", 0);
        }
        mav.addObject("solveData", data);
        return mav;
    }


    @RequestMapping("SolveAdd")
    public ModelAndView save(Solve solve) {
        String pid = solve.getPid().substring(0, solve.getPid().length());
        Problem problem = new Problem();
        problem.setPid(pid);
        Problem p = problemService.getById(problem.getPid());
        p.setIsSolve(1);
        problemService.updateById(p);
        ;
        solve.setName(p.getName());
        solve.setProContent(p.getDescript());
        solve.setProblem(problem);
        solve.setIssolve(1);

        if (solveService.save(solve)) {
            return new ModelAndView("/listSolve");
        } else {
            return new ModelAndView("/listProblem");
        }

    }

    @RequestMapping("findSolve")
    public ModelAndView find(Solve solve) {
        ModelAndView mav = new ModelAndView("/Main/ProblemSolve.jsp");

        List<Solve> data = solveService.listByName(solve.getName());
        if (data.size() != 0) {
            mav.addObject("solveflag", 1);
        } else {
            mav.addObject("solveflag", 0);
        }

        mav.addObject("solveData", data);
        return mav;
    }

    @RequestMapping("SolveDel")
    public ModelAndView SolveDel() {
        ModelAndView mav = new ModelAndView("/Main/ProblemSolveDel.jsp");
        List<Solve> data = solveService.list();
        if (data.size() != 0) {
            mav.addObject("solveflag", 1);
        } else {
            mav.addObject("solveflag", 0);
        }
        mav.addObject("solveData", data);
        return mav;
    }
    @RequestMapping("delSolve")
    @ResponseBody
    public Map del(Solve solve) {
        boolean state = false;
        Map map = new HashMap();
        try{
            solveService.removeById(solve.getSid());
            state=true;
            map.put("message","");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message",e.getStackTrace().toString());
        }
        map.put("state",state);

        map.put("data","");

        return  map;
    }

}
